package com.server.questionbank.snframe.mapper.Homework;

import com.server.questionbank.snframe.domain.Homework.HomeworkExercises;
import com.server.questionbank.snframe.domain.Homework.HomeworkPublish;
import com.server.questionbank.snframe.domain.Homework.THomework;
import com.server.questionbank.snframe.domain.Homework.UreceiveHwrec;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HomeworkMapper {
    // 获取作业
    @Select("SELECT * FROM T_homework WHERE homework_id = #{homeworkId}")
    public THomework getHomeworkById(Long homeworkId);
    @Select({
            "<script>",
            "SELECT * FROM T_homework ",
            "WHERE target_id = #{targetId} ",
            "<if test='homeworkType != null and homeworkType != \"\"'>",
            "AND homework_type = #{homeworkType} ",
            "</if>",
            "</script>"
    })
    List<THomework> getHomeworkListFlexible(
            @Param("targetId") Long targetId,
            @Param("homeworkType") String homeworkType
    );

    // 获取已发布的作业列表
    @Select("SELECT * FROM homework_publish WHERE publish_id = #{publishId}")
    public HomeworkPublish getHomeworkPublishById(Long publishId);
    @Select({
            "<script>",
            "SELECT homework_publish.* ",
            "FROM homework_publish, T_homework ",
            "WHERE homework_publish.homework_id = T_homework.homework_id ",
            "AND T_homework.target_id = #{targetId} ",
            "<if test='groupId != null'>",
            "AND homework_publish.target_id = #{groupId} ",
            "</if>",
            "<if test='homeworkType != null and homeworkType != \"\"'>",
            "AND T_homework.homework_type = #{homeworkType} ",
            "</if>",
            "</script>"
    })
    List<HomeworkPublish> getHomeworkPublishListFlexible(
            @Param("targetId") Long targetId,
            @Param("groupId") Long groupId,
            @Param("homeworkType") String homeworkType
    );

    // 获取指定作业的作业发布信息
    @Select("SELECT * FROM homework_publish WHERE homework_id = #{homeworkId}")
    public List<HomeworkPublish> getPublishHomeworkByHomeworkId(Long homeworkId);

    // 创建作业
    @Insert("INSERT INTO T_homework " +
            "(target_id, homework_name, homework_type, full_mark, exercise_count) " +
            "VALUES (#{targetId}, #{homeworkName}, #{homeworkType}, #{fullMark}, #{exerciseCount})")
    @Options(useGeneratedKeys = true, keyProperty = "homeworkId")
    public Integer createHomework(THomework homework);

    // 更新作业
    @Update("UPDATE T_homework " +
            "SET target_id = #{targetId}, homework_name = #{homeworkName}, " +
            "homework_type = #{homeworkType}, full_mark = #{fullMark}, exercise_count = #{exerciseCount} " +
            "WHERE homework_id = #{homeworkId}")
    public Integer updateHomework(THomework homework);

    // 删除作业
    @Delete("DELETE FROM T_homework WHERE homework_id = #{homeworkId}")
    public Integer deleteHomework(Long homeworkId);

    // 发布作业
    @Insert("INSERT INTO homework_publish " +
            "(publish_id, target_id, homework_id, publish_type, end_time, can_submit_over_time) " +
            "VALUES (#{publishId}, #{targetId}, #{homeworkId}, #{publishType}, #{endTime}, #{canSubmitOverTime})")
    public Integer publishHomework(HomeworkPublish homeworkPublish);

    // 更新发布作业
    @Update("UPDATE homework_publish SET " +
            "target_id = #{targetId}, homework_id = #{homeworkId}, publish_type = #{publishType}, " +
            "end_time = #{endTime},  can_submit_over_time = #{canSubmitOverTime} " +
            "WHERE publish_id = #{publishId}")
    public Integer updatePublishHomework(HomeworkPublish homeworkPublish);

    // 取消发布作业
    @Delete("DELETE FROM homework_publish WHERE publish_id = #{publishId}")
    public Integer cancelPublishHomework(Long publishId);

    // 获取用户提交数据
    @Select("SELECT * FROM ureceive_hwrec WHERE user_id = #{userId} AND publish_id = #{publishId}")
    public UreceiveHwrec getUserAnswerRecord(Long userId, Long publishId);

    // 获取作业中的题目
    @Select("SELECT * FROM homework_exercises WHERE homework_id = #{homeworkId}")
    public List<HomeworkExercises> getHomeworkExercises(Long homeworkId);

    // 添加作业题目
    @Insert("INSERT INTO homework_exercises (homework_id, exercises_id, exercise_score) " +
            "VALUES(#{homeworkId}, #{exercisesId}, #{exerciseScore})")
    public Integer addHomeworkExercises(HomeworkExercises homeworkExercises);

    // 删除作业题目
    @Delete("DELETE FROM homework_exercises WHERE homework_id = #{homeworkId}")
    public Integer deleteHomeworkExercises(Long homeworkId);
}
